﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2011/10/27 21:45:47
 *      Version:        1.0.1101
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;

namespace Sector7.Framework.Utility
{
    /// <summary>
    /// 垃圾回收工具类
    /// </summary>
    public class GCUtil
    {
        #region Public functions
        /// <summary>
        /// 关闭对象释放资源
        /// </summary>
        /// <param name="obj">对象实例</param>
        public static void Close(object obj)
        {
            if (obj != null)
            {
                if (ReflectionUtil.HasMethod(obj, "Close"))
                    ReflectionUtil.InvokeMethod(obj, "Close");

                if (ReflectionUtil.HasMethod(obj, "Dispose"))
                    ReflectionUtil.InvokeMethod(obj, "Dispose");

                obj = null;

                Collect();
            }
        }

        /// <summary>
        /// 释放对象占用资源
        /// </summary>
        /// <param name="obj">实现IDisposable接口对象</param>
        public static void Dispose(IDisposable obj)
        {
            if (obj != null)
            {                
                obj.Dispose();
                obj = null;

                Collect();
            }
        }

        /// <summary>
        /// 关闭IO流
        /// </summary>
        /// <param name="ioStream">IO流对象</param>
        public static void CloseStream(object ioStream)
        {
            if (ioStream != null)
            {
                if (ioStream is Stream)
                {
                    Stream stream = ioStream as Stream;

                    stream.Close();
                    stream.Dispose();
                }
                else if (ioStream is TextWriter)
                {
                    TextWriter textWriter = ioStream as TextWriter;

                    textWriter.Close();
                    textWriter.Dispose();
                }
                else if (ioStream is TextReader)
                {
                    TextReader textReader = ioStream as TextReader;

                    textReader.Close();
                    textReader.Dispose();
                }

                ioStream = null;
                Collect();
            }
        }

        /// <summary>
        /// 释放对象占用资源
        /// </summary>
        /// <param name="obj">实现IDisposable接口对象</param>
        public static void DisposeObject(IDisposable obj)
        {
            if (obj != null)
            {
                obj.Dispose();
                obj = null;

                Collect();
            }
        }

        /// <summary>
        /// 强制执行垃圾回收
        /// </summary>
        public static void Collect()
        {
            GC.Collect();
        }
        #endregion
    }
}
